package com.xxgc.cloud.api.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 公共的返回类
 */
@Data
@ApiModel
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
public class Result<T> implements Serializable {

    private static final String SUCCESSMSG = "操作成功!";

    @ApiModelProperty(value = "请求状态码1")
    private int code;

    @ApiModelProperty(value = "响应信息")
    private String msg;

    @ApiModelProperty(value = "当前系统版本")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String version;

    @ApiModelProperty(value = "当前环境")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String environment;

    //属性为 空（""） 或者为 NULL 都不序列化
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ApiModelProperty(value = "数据")
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> success(){
        return new Result<>(200,SUCCESSMSG,null);
    }

    public static <T> Result<T> success(T data){
        return new Result<>(200,SUCCESSMSG,data);
    }

    public static <T> Result<T> error(String error) {
        return new Result<>(500, error, null);
    }

}